package com.nit.bike.controller.basic;

import com.alibaba.fastjson.JSONObject;
import com.nit.bike.dto.basic.StationDTO;
import com.nit.bike.dto.utils.OpResultDTO;
import com.nit.bike.dto.utils.TableReqDTO;
import com.nit.bike.dto.utils.TableRspDTO;
import com.nit.bike.service.basic.StationService;
import com.nit.bike.utils.HttpRequestReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping(value = "/api/station")
public class StationController {
    @Resource
    private StationService stationService;
    private final Logger logger = LoggerFactory.getLogger(StationController.class);

    /**
     * 获取数据列表
     */
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    public TableRspDTO list4Table(@RequestBody TableReqDTO req) {
        TableRspDTO rsp = new TableRspDTO();
        try {
            rsp = stationService.list4Table(req);
        } catch (Exception e) {
            logger.error(e.toString());
        }
        return rsp;
    }

    /**
     * 新增记录
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public OpResultDTO add(@RequestBody StationDTO stationDTO) {
        OpResultDTO op = new OpResultDTO();
        try {
            op.setMessage(stationService.add(stationDTO).toString());
        } catch (Exception e) {
            logger.error(e.toString());
            op.setMessage("error");
        }
        return op;
    }

    /**
     * 更新记录
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public OpResultDTO update(@RequestBody StationDTO stationDTO) {
        OpResultDTO op = new OpResultDTO();
        try {
            op.setResult(stationService.update(stationDTO).toString());
        } catch (Exception e) {
            logger.error(e.toString());
            op.setMessage("error");
        }
        return op;
    }

    /**
     * 删除记录
     */
    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    public OpResultDTO remove(HttpServletRequest request) {
        OpResultDTO op = new OpResultDTO();
        try {
            JSONObject jsonRequest = HttpRequestReader.getJsonObject(request);
            String stationIds = jsonRequest.getString("stationIds");
            op.setMessage(stationService.remove(stationIds).toString());
        } catch (Exception e) {
            logger.error(e.toString());
            op.setMessage("error");
        }
        return op;
    }

    /**
     * 查询站点list
     */
    @RequestMapping(value = "/listStation", method = RequestMethod.POST)
    public OpResultDTO listStation() {
        OpResultDTO op = new OpResultDTO();
        try {
            op.setMessage("success");
            op.setResult(stationService.listStation());
        } catch (Exception e) {
            logger.error(e.toString());
            op.setMessage("error");
        }
        return op;
    }
}
